package com.sxt.practiceapplication.activities.test;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.TextView;

import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.utils.DisplayUtil;
import com.sxt.practiceapplication.utils.floatutil.FloatWindow;
import com.sxt.practiceapplication.utils.floatutil.MoveType;
import com.sxt.practiceapplication.utils.floatutil.Screen;
import com.sxt.practiceapplication.utils.floatutil.Util;


/**
 * 多窗体下的悬浮窗页面
 */
public class WindowActivity extends AppCompatActivity {

    Button startWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_window);
        startWindow = findViewById(R.id.start_window);
        if (!Util.hasAlertWindowPermission(this)) {
            requestAlertWindowPermission();
        }
        startWindow.setOnClickListener(this::onViewClicked);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.instance().releaseMediaPlayer();
        /*
          这里在返回主页的时候销毁了，因为不想和DEMO中其他页面冲突
         */
        FloatWindow.destroy();
    }

    private void requestAlertWindowPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //todo 用23以上编译即可出现canDrawOverlays
        if (Util.hasAlertWindowPermission(this)) {

        } else {
            this.finish();
        }
    }

    public void onViewClicked(View view) {
        if (view.getId() == R.id.start_window) {
            if (FloatWindow.get() != null) {
                return;
            }
//            FloatPlayerView floatPlayerView = new FloatPlayerView(getApplicationContext());
            TextView textView = new TextView(this);
            textView.setWidth(DisplayUtil.px2dip(200, DisplayUtil.getScreenDensity(this)));
            textView.setHeight(DisplayUtil.px2dip(200, DisplayUtil.getScreenDensity(this)));
            textView.setText("emmm");
            FloatWindow
                    .with(getApplicationContext())
                    .setView(textView)
                    .setWidth(Screen.width, 0.3f)
                    .setHeight(Screen.width, 0.3f)
                    .setX(Screen.width, 0.8f)
                    .setY(Screen.height, 0.3f)
                    .setMoveType(MoveType.slide)
                    .setFilter(false)
                    .setMoveStyle(500, new BounceInterpolator())
                    .build();
            FloatWindow.get().show();
        }
    }
}
